/**
 * 洞察流程信息存储，对外提供获取流程节点配置参数的方法 
 */
var MineFlowInfo = {};



/**
 * 挖掘洞察控制类
 */
function MineViewlingController(pid) {
	var oViewTabs =  $('#tempo-mine-viewing-tabs');
	var oOverView = $('#tempo-mine-viewing-overview');
	var oInputView = $('#tempo-mine-viewing-inputview');
	var oProcessView = $('#tempo-mine-viewing-processview');
	var oResultview = $('#tempo-mine-viewing-resultview');
	var oView = $('#tempo-mine-viewing-resultshow');
	var mask = null;
	var showMaskOpt = function(opt){
		if(opt == 'show'){
			mask = $("<div/>").tempoprogressbar();
		}else{
			if(mask) {
				mask.tempoprogressbar('close');
			}
		}
	}

	
	/**
	 * 1.生成节点和端口洞察显示框架界面。<br>
	 * 2.根据节点和端口提取对应端口的洞察显示界面<br>
	 * 
	 * @param oView 父容器
	 * @param filterCondition过滤条件
	 * @portsArray 需要显示的端口类型数组
	 * */
	var loadMineViewingInsight = function(oView,filterCondition,portsArray, callback) {
		showMaskOpt('show');
		$.ajax({
			url:contextPath+"/api/ai/mine/searchInsightInfo",
			data:{
				flowId: pid
			},
			type:'post',
			async:true,
			success:function(insightResult){
				showMaskOpt('hide');
				if (insightResult) {
					MineViewingInsightNodePortController(showMaskOpt, oView,filterCondition,insightResult,portsArray, callback);
				}
			},			
			error:function(){
			}
		});
	}
	
	var flowOverView = function(){
		//综述
		
	}
	
	
	
	var bindEvent = function(data){
		/**
		 * 概览洞察点击事件处理函数
		 */
		oOverView.unbind('click').bind('click', (function(event) {
			var that = this;
			$.ajax({
				url:contextPath+"/api/ai/mine/searchProcessInsightInfo",
				data:{
					flowId: pid
				},
				success : function(result){
					oView.empty();
					if(!result || !result.processData) return;
					var insightData = result.processData;
			        $(that).addClass('viewtabsbackgroundcolor').siblings().removeClass('viewtabsbackgroundcolor');
					var container = $('<div id="containerTab">').appendTo(oView).css('height', '100%');
					var summaryPanel = $('<div id="summary-container">').appendTo(container).accordionpanel({
						expanded : true,
						title : TempoUtils.locale("mine.summarize"), 
//						link : '预览',
						linkHandler : function(){
							var viewDialog = $('<div>');
							window.open(contextPath + "/api/ai/mine/view/insight/preview/page#" + pid, '_blank')
						}
					});
					
					var descDiv = $('<div id="tempo-mine-summary-desc">');
					summaryPanel.accordionpanel('addContent', descDiv);
					var descData = insightData.desc;
					descData.PNAME = result.PNAME;
					descData.THEMENAME = result.THEMENAME;
					rendOverViewDesc(descDiv, descData);
					
					var flowDiv = $('<div id="tempo-mine-summary-flow">');
					summaryPanel.accordionpanel('addContent', flowDiv);
					loadInputdata(pid, flowDiv, flowDiv, cacheFlowData);
					
					
//					var inputData = insightData.input;
//					renderSummaryInput(function(){
//						var inputPanel = $('<div id="input-container">').appendTo(container).accordionpanel({
//							expanded : true,
//							title : '输入'
//						});
//						var inputDiv = $('<div id="tempo-mine-summary-input">');
//						inputPanel.accordionpanel('addContent', inputDiv);
//						return inputDiv;
//					}, inputData);
//					
//					
//					var preprocessData = insightData.preprocess;
//					var dealPanel = $('<div id="deal-container">').appendTo(container).accordionpanel({
//						expanded : true,
//						title : '处理'
//					});
//					var dealDiv = $('<div id="tempo-mine-summary-input">');
//					dealPanel.accordionpanel('addContent', dealDiv);
//					rendPreprocessTable(dealDiv, preprocessData);
//					
//					var resultData = insightData.result;
//					var resultPanel = $('<div id="input-result">').appendTo(container).accordionpanel({
//						expanded : true,
//						title : '结果'
//					});
//					var resultDiv = $('<div id="tempo-mine-summary-input">');
//					resultPanel.accordionpanel('addContent', resultDiv);
//					renderSummaryResult(resultDiv, resultData);
				}
			});
		}));
		
		/**
		 * 输入洞察点击事件处理函数
		 */
		oInputView.unbind('click').bind('click',(function() {
			$(this).addClass('viewtabsbackgroundcolor').siblings().removeClass('viewtabsbackgroundcolor');
			loadMineViewingInsight(oView,'data_input',[window.mineConstant.PORT_DATASET]);
		}));
		
		/**
		 * 过程洞察点击事件处理函数
		 */
		oProcessView.unbind('click').bind('click',(function() {
	        $(this).addClass('viewtabsbackgroundcolor')
	    	.siblings().removeClass('viewtabsbackgroundcolor');
			oView.html("");
			
			var oProcessViewDiv = $('<div id="tempo-mine-process-view-view">').appendTo(oView);
			
			processTypeSelect = function(type){
				oProcessViewDiv.html("");
				if(type == 'preprocess'){
					loadMineViewingInsight(oProcessViewDiv,'preprocess',[window.mineConstant.PORT_NODE_DEFAULT,window.mineConstant.PORT_DATASET], processTypeSelect);
				}
				if(type == 'datamine'){
					loadMineViewingInsight(oProcessViewDiv,'datamine',[window.mineConstant.PORT_MODEL,window.mineConstant.PORT_DATASET], processTypeSelect);
				}
				if(type == 'dm_model'){
					loadMineViewingInsight(oProcessViewDiv,'dm_model',[window.mineConstant.PORT_MODEL,window.mineConstant.PORT_DATASET], processTypeSelect);
				}
				if(type == 'minechart'){
					loadMineViewingInsight(oProcessViewDiv,'minechart',[window.mineConstant.PORT_GRAPH], processTypeSelect);
				}
				if(type == 'dm_control'){
					loadMineViewingInsight(oProcessViewDiv,'dm_control',[window.mineConstant.PORT_MODEL,window.mineConstant.PORT_DATASET], processTypeSelect);
				}
			}
			
			loadMineViewingInsight(oProcessViewDiv,'preprocess',[window.mineConstant.PORT_NODE_DEFAULT,window.mineConstant.PORT_DATASET], processTypeSelect);
			
	        $('#tempo-mine-viewing-dataprocess').unbind('click').bind('click',(function(){
	        	if (!$(this).hasClass('tempo-mine-viewing-result-style-sel')) {
					$(this).addClass('tempo-mine-viewing-result-style-sel');
				}
				$(this).siblings().removeClass('tempo-mine-viewing-result-style-sel');
				loadMineViewingInsight(oProcessViewDiv,'preprocess',[window.mineConstant.PORT_NODE_DEFAULT,window.mineConstant.PORT_DATASET]);
	        }));
	        
			$('#tempo-mine-viewing-dataprocess-alg').unbind('click').bind('click',(function(){
				if (!$(this).hasClass('tempo-mine-viewing-result-style-sel')) {
					$(this).addClass('tempo-mine-viewing-result-style-sel');
				}
				$(this).siblings().removeClass('tempo-mine-viewing-result-style-sel');
				
				loadMineViewingInsight(oProcessViewDiv,'datamine',[window.mineConstant.PORT_MODEL]);
			}));
			
			$('#tempo-mine-viewing-dataprocess-model').unbind('click').bind('click',(function(){
				if (!$(this).hasClass('tempo-mine-viewing-result-style-sel')) {
					$(this).addClass('tempo-mine-viewing-result-style-sel');
				}
				$(this).siblings().removeClass('tempo-mine-viewing-result-style-sel');
				oProcessViewDiv.html("");
				loadMineViewingInsight(oProcessViewDiv,'dm_model',[window.mineConstant.PORT_MODEL,window.mineConstant.PORT_DATASET]);
			}));
			
			$('#tempo-mine-viewing-dataprocess-pic').unbind('click').bind('click',(function(){
				if (!$(this).hasClass('tempo-mine-viewing-result-style-sel')) {
					$(this).addClass('tempo-mine-viewing-result-style-sel');
				}
				$(this).siblings().removeClass('tempo-mine-viewing-result-style-sel');
				oProcessViewDiv.html("");
				loadMineViewingInsight(oProcessViewDiv,'minechart',[window.mineConstant.PORT_GRAPH]);
			}));
			
			$('#tempo-mine-viewing-dataprocess-control').unbind('click').bind('click',(function(){
				if (!$(this).hasClass('tempo-mine-viewing-result-style-sel')) {
					$(this).addClass('tempo-mine-viewing-result-style-sel');
				}
				$(this).siblings().removeClass('tempo-mine-viewing-result-style-sel');
				oProcessViewDiv.html("");
				loadMineViewingInsight(oProcessViewDiv,'dm_control',[window.mineConstant.PORT_MODEL,window.mineConstant.PORT_DATASET]);
			}));
		}));
		
		/**
		 * 结果洞察点击事件处理函数
		 */
		oResultview.unbind('click').bind('click',(function() {
			$(this).addClass('viewtabsbackgroundcolor').siblings().removeClass('viewtabsbackgroundcolor');
			loadMineViewingInsight(oView,'result',['END', window.mineConstant.PORT_DATASET,window.mineConstant.PORT_MODEL,window.mineConstant.PORT_GRAPH]);
		}));
	}
	
	
	
	
	/**
	 * 缓存流程信息，用来节点信息获取
	 * */
	var cacheFlowData = function(param) {
		var flowData = JSON.parse(param);
		var data = processChildrenPosition(flowData.children);
		var height = data.maxY-data.minY + 180;
		$('#tempo-mine-summary-flow').height(height);
		MineFlowInfo = (function() {
			return {
				getParameters: function(id) {
					/**
					 * 获取节点配置信息（包含子流程内部节点）
					 */
					var _findData = function(pathId, navData){
						var navId =  navData.eid; 
						if(navData.process){
							navId = navData.process.id;
							navData = navData.process.flow;
						}
						if(navId == pathId){
							return navData.parameters;
						}else{
							if(navData && navData.children){
								var navs =  navData.children;
								if(navs){
									for (var i = 0; i < navs.length; i++) {
										var nav = navs[i];
										var data = _findData(pathId, nav);
										if(data != null){
											return data;
										}
									}
								}
							}
						}
					};
					if(flowData && flowData.children) {
						return _findData(id, flowData);
					}
					return {};
				}
			}
		})();
	}
	
	
	
	
	var init = function(){
//		loadInsightData();
		bindEvent();
		oOverView.trigger('click');
	}
	//初始化
	init();
}